﻿using Architecture.Domain.Services.Interface;
using Architecture.Domian.Common;
using Architecture.Domian.DTO;
using Architecture.Domian.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.Api.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        /// <summary>
        /// 菜单服务
        /// </summary>
        private readonly IMenuService _menuService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="menuService">菜单服务对象</param>
        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 根据菜单父级Id获取菜单列表
        /// </summary>
        /// <returns>菜单列表</returns>
        [HttpGet]
        public List<MenuListDTO> GetMenuListDTO()
        {
            var res = _menuService.GetMenuListDTO();
            return res;
        }

        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="menuInfo">菜单实体</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public int MenuInsert(MenuInfo menuInfo)
        {
            var res = _menuService.MenuInsert(menuInfo).Result;
            return res;
        }

        /// <summary>
        /// 菜单多条件查询分页对象列表
        /// </summary>
        /// <param name="size">当前页容量</param>
        /// <param name="index">当前页索引</param>
        /// <returns>菜单多条件查询分页对象列表</returns>
        [HttpGet]
        public PageResult<MenuInfoDTO> GetPage(int size, int index)
        {
            var res = _menuService.GetPage(size, index).Result;
            return res;
        }

        /// <summary>
        /// 根据菜单Id获取菜单信息
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <returns>菜单信息</returns>
        [HttpGet]
        public MenuInfo GetMenuInfo(int menuId)
        {
            var res = _menuService.GetMenuInfo(menuId).Result;
            return res;
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="menu">菜单实体</param>
        /// <returns>受影响行数</returns>
        [HttpPut]
        public int UpdateMenuInfo(MenuInfo menu)
        {
            var res = _menuService.UpdateMenuInfo(menu).Result;
            return res;
        }

        /// <summary>
        /// 根据菜单Id删除菜单信息(无子级菜单)
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public int DeleteMenuInfo(int menuId)
        {
            var res = _menuService.DeleteMenuInfo(menuId).Result;
            return res;
        }

        /// <summary>
        /// 根据菜单Id删除菜单信息(含子级菜单)
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public int DeleteMenu(int menuId)
        {
            var res = _menuService.DeleteMenu(menuId).Result;
            return res;
        }

        /// <summary>
        /// 获取角色树形数据
        /// </summary>
        /// <returns>角色树形数据列表</returns>
        [HttpGet]
        public List<TreeList> GetRoleTree()
        {
            var res = _menuService.GetRoleTree();
            return res;
        }

        /// <summary>
        /// 批量添加角色权限关系信息
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="menuIds">菜单Id集合</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public int PressionInsert(int roleId, List<int> menuIds)
        {
            var res = _menuService.PressionInsert(roleId, menuIds).Result;
            return res;
        }

        /// <summary>
        /// 根据角色Id获取菜单Id集合
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns>菜单Id集合</returns>
        [HttpGet]
        public List<int> GetMenuIdsByRoleId(int roleId)
        {
            var res = _menuService.GetMenuIdsByRoleId(roleId);
            return res;
        }


        /// <summary>
        /// 根据菜单名称查询菜单列表
        /// </summary>
        /// <param name="menuName">菜单名称</param>
        /// <returns>菜单列表（包含一级子项）</returns>
        [HttpGet]
        public List<MenuListDTO> GetMenuListDTOByMenuName(string? menuName)
        {
            var res = _menuService.GetMenuListDTOByMenuName(menuName).Result;
            return res;
        }
    }
}
